
from multiprocessing import Process, Pool, freeze_support
import os, time
#pool进程池
def foo(i):
    time.sleep(2)
    print('in process', os.getpid())
    return i+100

def bar(arg):
    print('-->exec done:', arg, os.getpid())

if __name__ == '__main__':
    pool = Pool(processes=5)       #最多允许5个进程放入进程池
    print('主进程', os.getpid())
    for i in range(10):
        pool.apply_async(func=foo, args=(i,), callback=bar)     #callback回调参数
        # pool.apply_async(func=foo, args=(i,))       #apply_async 并行
        # pool.apply(func=foo, args=(i,))       #apply 串行

    print('end')
    pool.close()
    pool.join()  #先close后join，进程池中执行完毕后再关闭，如果不写，程序直接关闭


